/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Code;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.input.ChaseCamera;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;

/**
 *
 * @author Owner
 */
public class Character implements AnimEventListener{
    private AnimChannel channel;
    private AnimControl control;
    Node player;
    private boolean left = false, right = false, up = false, down = false, forward = false;
    private Vector3f walkDirection = new Vector3f(0,0,0);
    private Vector3f viewDirection = new Vector3f(0,0,0);
    private String movement = "Walking";
    private CharacterControl character;
    private Camera cam;
    private ChaseCamera chaseCam;
    public CharacterControl characterController;
    
    public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
    if (animName.equals("Walk")) {
        if (movement == "Standing")
        {
      channel.setAnim("stand", 0.50f);
      channel.setLoopMode(LoopMode.DontLoop);
      channel.setSpeed(1f);
        }else if (movement == "Walking")
        {
            channel.setAnim("Walk",0.50f);
        }
    }
  }
    public void load(InputManager inputManager, AssetManager assetManager, Node rootNode, Camera camera){
        initKeys(inputManager);
        player = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
        player.setLocalScale(0.5f);
        player.setLocalTranslation(400, 0, 0);
        rootNode.attachChild(player);
        control = player.getControl(AnimControl.class);
        control.addListener(this);
        channel = control.createChannel();
        channel.setAnim("stand");
        cam = camera;
        chaseCam = new ChaseCamera(cam, player, inputManager);
    }
    
  public void update(){
  Vector3f camDir = cam.getDirection().clone().multLocal(0.25f);
  Vector3f camLeft = cam.getLeft().clone().multLocal(0.25f);
  camDir.y = 0;
  camLeft.y = 0;
  walkDirection.set(0, 0, 0);
  viewDirection.set(camDir);
  if (left)  walkDirection.addLocal(camLeft);
  if (right) walkDirection.addLocal(camLeft.negate());
  if (forward) walkDirection.addLocal(camDir);
  if (down) walkDirection.addLocal(camDir.negate());
  characterController.setWalkDirection(walkDirection);
  characterController.setViewDirection(viewDirection);
      
  }
 
  public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
    // unused
  }
  
  public void setAnimation(String name, float speed)
  {
      if (!channel.getAnimationName().equals(name))
      {
      }
  }
 
  /** Custom Keybinding: Map named actions to inputs. */
  private void initKeys(InputManager inputManager) {
    inputManager.addMapping("Walk", new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addMapping("Forward", new KeyTrigger(KeyInput.KEY_W));
    inputManager.addListener(actionListener, "Walk");
    inputManager.addListener(actionListener, "Forward");
  }
  private ActionListener actionListener = new ActionListener() {
    public void onAction(String name, boolean keyPressed, float tpf) {
      if (name.equals("Walk") && !keyPressed) {
        if (!channel.getAnimationName().equals("Walk")) {
          channel.setAnim("Walk", 0.50f);
          channel.setLoopMode(LoopMode.Loop);
        }
      }else if (name.equals("Forward"))
      {
          if (keyPressed)
          { forward = true;
          channel.setAnim("Walk");
          }else{
              forward = false;
              channel.setAnim("stand");
          }
      }
    }
  };
}
